package com.daji.concurrency.multithread_fundamental;

public class A03_Interrupt {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        //大吉注：如果用下面注释掉的lambda方法(实现接口)建立线程，会报错。找不到isInterrupted。这个isInterrupted被继承得来。
        /*Thread t0 = new Thread(()->{
            int n = 0;
            while (! isInterrupted()) {
                n ++;
                System.out.println(n + " hello!");
            }
        }).start();*/
        t.start();
        //大吉注：将下面的1毫秒改成10，重新运行，看看效果：
        Thread.sleep(1); // 暂停1毫秒
        t.interrupt(); // 中断t线程
        t.join(); // 等待t线程结束
        System.out.println("end");
    }
}

class MyThread extends Thread {
    public void run() {
        int n = 0;
        while (! isInterrupted()) {
            n ++;
            System.out.println(n + " hello!");
        }
    }
}
